﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class Representative : DBFObject, IDBObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        private int m_OIndividual;

        public Individual OIndividual
        {
            get;
            set;
        }

        String m_NumberProxy = "";
        public string NumberProxy
        {
            get
            {
                return m_NumberProxy;
            }
            set
            {
                m_NumberProxy = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberProxy"));
            }
        }


        DateTime? m_DateIssue = null;
        public DateTime? DateIssue
        {
            get
            {
                return m_DateIssue;
            }
            set
            {
                m_DateIssue = value;
                OnPropertyChanged(new PropertyChangedEventArgs("DateIssue"));
            }
        }

        String m_IssueBy = "";
        public string IssueBy
        {
            get
            {
                return m_IssueBy;
            }
            set 
            {
                m_IssueBy = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IssueBy"));
            }
        }





        public Representative()
        {
            NameTable = "REPRESENTATIVE_TABLE";

            OIndividual = new Individual();

            NumberProxy = "";
            DateIssue = null;
            IssueBy = "";

        }

        Object IDBObject.Save()
        {
            try
            {
                Parameters = new Dictionary<string, object>();

                Parameters.Add("ID_INDIVIDUAL", (OIndividual as IDBObject).Save());

                Parameters.Add("NUMBER_PROXY", NumberProxy);
                Parameters.Add("DATA_ISSUE", DateIssue);
                Parameters.Add("ISSUE_BY", IssueBy);

                return SaveObject();
            }
            catch (SystemException err)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД с сообщением {1}", this.ToString(), err.Message));
            }
        }

        void IDBObject.Load(object ID)
        {
            try
            {
                Parameters = new Dictionary<string, object>();
                Parameters.Add("ID_INDIVIDUAL", m_OIndividual);

                Parameters.Add("NUMBER_PROXY", NumberProxy);
                Parameters.Add("DATA_ISSUE", DateIssue);
                Parameters.Add("ISSUE_BY", IssueBy);

                LoadObject(ID);
                ((OIndividual = new Individual()) as IDBObject).Load(Parameters["ID_INDIVIDUAL"]);

                NumberProxy = (string)Parameters["NUMBER_PROXY"];
                DateIssue =   Parameters["DATA_ISSUE"] == null || Parameters["DATA_ISSUE"].ToString() == String.Empty? null : (DateTime?)DateTime.Parse( Parameters["DATA_ISSUE"].ToString() );
                IssueBy = (string)Parameters["ISSUE_BY"];
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД", this.ToString()));
            }
        }


        void IDBObject.Update()
        {
            (OIndividual as IDBObject).Update();

            Parameters = new Dictionary<string, object>();
            Parameters.Add("NUMBER_PROXY", NumberProxy);
            Parameters.Add("DATA_ISSUE", DateIssue);
            Parameters.Add("ISSUE_BY", IssueBy);

            UpdateObject();
        }
    }
}
